<?php

namespace App\Http\Controllers\Export;

use App\Http\Controllers\Controller;
use App\Models\SatisfactionSurvey;
use App\Models\SatisfactionSurveyReply;
use Maatwebsite\Excel\Facades\Excel;

/**
 * excel导出导入功能
 */
class SatisfactionSurveyReplyExport extends Controller
{

    public $model = null;
    public $replyModel = null;


    public function __construct()
    {
        parent::__construct();

        $this->model = new SatisfactionSurvey();
        $this->replyModel = new SatisfactionSurveyReply();
    }

    /**
     * 满意度调查用户回复列表导出
     * @param limit 限制条数 默认为 10
     * @param survey_id 问卷调查id
     * @param keywords 检索内容
     * @param is_satisfaction int 是否满意  1 满意  2 不满意
     * @param status int 是否核销  1 以核销  2 未核销（时间已过）3 待核销
     * @param start_time datetime 活动开始时间    数据格式  年月日
     * @param end_time datetime 活动结束时间
     * @param qr_code datetime 核销码
     */
    public function index()
    {
        $survey_id = $this->request->survey_id;
        if (empty($survey_id)) {
            return $this->returnApi(201, "问卷id不能为空");
        }

        $keywords = $this->request->keywords;

        // 获取问卷调查是否存在
        $res = $this->model->select('id', 'survey_name')->where("is_del", 1)->where('id', $survey_id)->first();

        if (empty($res)) {
            $data = [];
        } else {
            $keywords = $this->request->keywords;
            $is_satisfaction = $this->request->is_satisfaction;
            $status = $this->request->status;
            $start_time = $this->request->start_time;
            $end_time = $this->request->end_time;
            $qr_code = $this->request->qr_code;

            // 获取数据
            $data = $this->replyModel->lists($survey_id, null, $keywords, $is_satisfaction, $status, $start_time, $end_time, $qr_code, 99999);

            //执行导出
            $data = $this->setData($data['data']); //要导入的数据
        }
        //设置表头
        $row = [[
            "id" => '序号',
            "nickname" => '用户昵称',
            "username" => '用户名',
            "tel" => '电话号码',
            "is_satisfaction" => '是否满意',
            "create_time" => '回复时间',
        ]];

        $title = '满意度调查用户回复数据导出-' . date('YmdHis');

        return $this->exportData($data, $title, $row);
    }


    /**
     * 导出数据
     *
     * @param [type] $data
     * @return void
     */
    public function exportData($data, $title, $row)
    {
        $header = $row; //导出表头
        $excel = new Export($data, $header, $title);
        $excel->setColumnWidth(['A' => 10, 'B' => 20, 'C' => 20, 'D' => 20, 'E' => 20, 'F' => 20, 'G' => 20, 'H' => 20, 'I' => 20, 'J' => 20, 'K' => 20, 'L' => 20, 'M' => 20, 'N' => 20, 'O' => 20, 'P' => 20, 'Q' => 20, 'R' => 20, 'S' => 20, 'T' => 20, 'U' => 20, 'V' => 20, 'W' => 20, 'X' => 20, 'Y' => 20, 'Z' => 20]);
        $excel->setRowHeight([1 => 30]);
        // $excel->setFreezePane('A2');
        // $excel->setFont(['A1:Z1265' => '宋体']);
        // $excel->setFontSize(['A1:I1' => 14,'A2:Z1265' => 10]);
        // $excel->setBold(['A1:Z2' => true]);
        // $excel->setBackground(['A1:A1' => '808080','C1:C1' => '708080']);
        // $excel->setMergeCells(['A1:I1']);
        // $excel->setBorders(['A2:D5' => '#000000']);
        return Excel::download($excel, $title . '.xlsx');
    }


    /**
     * 处理 终端书籍列表 数据
     */
    public function setData($data)
    {
        $excel_data = [];
        /*设置excel内容*/
        foreach ($data as $key => $val) {
            $excel_data[$key]['id'] = $key + 1;
            $excel_data[$key]['nickname'] = $val['nickname'];
            $excel_data[$key]['username'] = $val['username'];
            $excel_data[$key]['tel'] = $val['tel'];
            $excel_data[$key]['is_satisfaction'] = $val['is_satisfaction'] == 1 ? "满意" : '不满意';
            $excel_data[$key]['create_time'] = $val['create_time'];
        }

        return $excel_data;
    }
}
